package com.cgly.medical.service.impl;

import com.cgly.medical.domain.AppointmentTime;
import com.cgly.medical.entity.PatientHistoryEntity;
import com.cgly.medical.mapper.AppointmentTimeMapper;
import com.cgly.medical.mapper.PatientHistoryMapper;
import com.cgly.medical.model.CompanyPolicyModel;
import com.cgly.medical.model.PatientHistoryModel;
import com.cgly.medical.param.PatientHistoryParam;
import com.cgly.medical.service.PatientHistoryService;
import com.cgly.medical.util.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 黄浩然
 */
@Service
public class PatientHistoryServiceImpl implements PatientHistoryService {

//    @Autowired
    @Resource
    private PatientHistoryMapper patientHistoryMapper;
    @Resource
    private AppointmentTimeMapper appointmentTimeMapper;

    public Msg getAllPatientHistory(PatientHistoryParam param) {
        if (param.getSize() == 0) {
            param.setSize(1);
        }
        PageHelper.startPage(param.getPn(), param.getSize());
        List<PatientHistoryModel> list = patientHistoryMapper.getAllPatientHistory(param);
        PageInfo<PatientHistoryModel> info = new PageInfo<>(list, 5);
        for (PatientHistoryModel model:list) {
            System.out.println(model.getDoctor().getName());
            System.out.println(model.getCreateTime());
        }

        return Msg.success().data("patientHistoryInfo",info);
    }

    @Override

    public Msg addPatientHistory(Integer time,PatientHistoryParam param) {
        AppointmentTime appointmentTime=new AppointmentTime();
        appointmentTime.setDoctorId(param.getDoctorId());
        int year = new DateTime().getYear();
        int month = new DateTime().getMonthOfYear();
        int day = new DateTime().getDayOfMonth();
        if(time==8){
            appointmentTime.setEight(1);
            param.setCreateTime(new DateTime(year, month, day, 8, 0, 0, 0).toDate());
        } else if (time == 9) {
            appointmentTime.setNine(1);
            param.setCreateTime(new DateTime(year, month, day, 9, 0, 0, 0).toDate());
        } else if (time == 10) {
            appointmentTime.setTen(1);
            param.setCreateTime(new DateTime(year, month, day, 10, 0, 0, 0).toDate());
        }else if (time == 11) {
            appointmentTime.setEleven(1);
            param.setCreateTime(new DateTime(year, month, day, 11, 0, 0, 0).toDate());
        }else if (time == 14) {
            appointmentTime.setFourteen(1);
            param.setCreateTime(new DateTime(year, month, day, 14, 0, 0, 0).toDate());
        }else if (time == 15) {
            appointmentTime.setFifteen(1);
            param.setCreateTime(new DateTime(year, month, day, 15, 0, 0, 0).toDate());
        }else if (time == 16) {
            appointmentTime.setSixteen(1);
            param.setCreateTime(new DateTime(year, month, day, 16, 0, 0, 0).toDate());
        }else if (time == 17) {
            appointmentTime.setSeventeen(1);
            param.setCreateTime(new DateTime(year, month, day, 17, 0, 0, 0).toDate());
        }
        appointmentTimeMapper.updateAppointmentTime(appointmentTime);
        Date date = new DateTime().toDate();
        param.setUpdateTime(date);
//        param.setUpdateTime(new DateTime().toDate());
        PatientHistoryEntity entity = new PatientHistoryEntity();
        BeanUtils.copyProperties(param,entity);
        entity.setStatus(0);
        int i = patientHistoryMapper.addPatientHistory(entity);
        if (i > 0) {
            return Msg.success().mess("添加成功");
        }else{
            return Msg.fail().mess("添加失败");
        }
    }

    @Override
    public Msg getPatientHistory(Long id) {
        PatientHistoryModel patientHistory = patientHistoryMapper.getPatientHistoryById(id);
        return Msg.success().data("patientHistory",patientHistory);
    }

    @Override
    public Msg updatePatientHistory(PatientHistoryParam param) {
        param.setUpdateTime(new DateTime().toDate());
        PatientHistoryEntity entity = new PatientHistoryEntity();
        BeanUtils.copyProperties(param,entity);
        int i = patientHistoryMapper.updatePatientHistory(entity);
        if (i > 0) {
            return Msg.success().mess("修改成功");
        }else{
            return Msg.fail().mess("修改失败");
        }
    }

    @Override
    public Msg deletePatientHistory(Long id) {
        int i = patientHistoryMapper.deletePatientHistory(id);
        if (i > 0) {
            return Msg.success().mess("删除成功");
        }else{
            return Msg.fail().mess("删除失败");
        }
    }

    @Override
    public Msg getPatientHistoryByDoctorId(PatientHistoryParam param) {
        if (param.getSize() == 0) {
            param.setSize(1);
        }
        PageHelper.startPage(param.getPn(), param.getSize());
        List<PatientHistoryModel> list = patientHistoryMapper.getPatientHistoryByDoctorId(param);
        PageInfo<PatientHistoryModel> info = new PageInfo<>(list, 5);
        for (PatientHistoryModel model:list) {
            System.out.println(model.getDoctor().getName());
            System.out.println(model.getCreateTime());
        }

        return Msg.success().data("patientHistoryInfo",info);
    }
}
